

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>nlp_architect.models.bist.mstlstm &mdash; NLP Architect by Intel® AI Lab 0.5.2 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../../_static/language_data.js"></script>
        <script type="text/javascript" src="../../../../_static/install.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/nlp_arch_theme.css" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:100,900" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html">
          

          
            
            <img src="../../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../quick_start.html">Quick start</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../tutorials.html">Jupyter Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../model_zoo.html">Model Zoo</a></li>
</ul>
<p class="caption"><span class="caption-text">NLP/NLU Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../tagging/sequence_tagging.html">Sequence Tagging</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../sentiment.html">Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../bist_parser.html">Dependency Parsing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../intent.html">Intent Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../lm.html">Language Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../information_extraction.html">Information Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../transformers.html">Transformers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../archived/additional.html">Additional Models</a></li>
</ul>
<p class="caption"><span class="caption-text">Optimized Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../quantized_bert.html">Quantized BERT</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../transformers_distillation.html">Transformers Distillation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../sparse_gnmt.html">Sparse Neural Machine Translation</a></li>
</ul>
<p class="caption"><span class="caption-text">Solutions</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../absa_solution.html">Aspect Based Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../term_set_expansion.html">Set Expansion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../trend_analysis.html">Trend Analysis</a></li>
</ul>
<p class="caption"><span class="caption-text">For Developers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../generated_api/nlp_architect_api_index.html">nlp_architect API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../developer_guide.html">Developer Guide</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">NLP Architect by Intel® AI Lab</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
          <li><a href="../../models.html">nlp_architect.models</a> &raquo;</li>
        
      <li>nlp_architect.models.bist.mstlstm</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for nlp_architect.models.bist.mstlstm</h1><div class="highlight"><pre>
<span></span><span class="c1"># ******************************************************************************</span>
<span class="c1"># Copyright 2017-2018 Intel Corporation</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#     http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ******************************************************************************</span>
<span class="c1"># pylint: disable=no-name-in-module</span>
<span class="c1"># pylint: disable=too-many-instance-attributes</span>
<span class="c1"># pylint: disable=too-many-arguments</span>
<span class="c1"># pylint: disable=too-many-statements</span>
<span class="c1"># pylint: disable=too-many-locals</span>
<span class="c1"># pylint: disable=too-many-branches</span>

<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span>
<span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="n">itemgetter</span>

<span class="kn">from</span> <span class="nn">dynet</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">ParameterCollection</span><span class="p">,</span>
    <span class="n">AdamTrainer</span><span class="p">,</span>
    <span class="n">LSTMBuilder</span><span class="p">,</span>
    <span class="n">tanh</span><span class="p">,</span>
    <span class="n">logistic</span><span class="p">,</span>
    <span class="n">rectify</span><span class="p">,</span>
    <span class="n">cmult</span><span class="p">,</span>
    <span class="n">SimpleRNNBuilder</span><span class="p">,</span>
    <span class="n">concatenate</span><span class="p">,</span>
    <span class="n">np</span><span class="p">,</span>
    <span class="n">renew_cg</span><span class="p">,</span>
    <span class="n">esum</span><span class="p">,</span>
<span class="p">)</span>

<span class="kn">from</span> <span class="nn">nlp_architect.data.conll</span> <span class="kn">import</span> <span class="n">ConllEntry</span>
<span class="kn">from</span> <span class="nn">nlp_architect.models.bist</span> <span class="kn">import</span> <span class="n">decoder</span>
<span class="kn">from</span> <span class="nn">nlp_architect.models.bist.utils</span> <span class="kn">import</span> <span class="n">read_conll</span>


<span class="c1"># Things that were changed from the original:</span>
<span class="c1"># - Added input validation</span>
<span class="c1"># - Updated function and object names to dyNet 2.0.2 and Python 3</span>
<span class="c1"># - Removed external embeddings option</span>
<span class="c1"># - Reformatted code and variable names to conform with PEP8</span>
<span class="c1"># - Added dict_to_obj()</span>
<span class="c1"># - Added option for train() to get ConllEntry input</span>
<span class="c1"># - Added legal header</span>
<span class="c1"># - Disabled some style checks</span>


<div class="viewcode-block" id="MSTParserLSTM"><a class="viewcode-back" href="../../../../generated_api/nlp_architect.models.bist.html#nlp_architect.models.bist.mstlstm.MSTParserLSTM">[docs]</a><span class="k">class</span> <span class="nc">MSTParserLSTM</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Underlying LSTM model for MSTParser used by BIST parser.&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">vocab</span><span class="p">,</span> <span class="n">w2i</span><span class="p">,</span> <span class="n">pos</span><span class="p">,</span> <span class="n">rels</span><span class="p">,</span> <span class="n">options</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="n">options</span> <span class="o">=</span> <span class="n">_dict_to_obj</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="s2">&quot;Values&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">ParameterCollection</span><span class="p">()</span>
        <span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">trainer</span> <span class="o">=</span> <span class="n">AdamTrainer</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">activations</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;tanh&quot;</span><span class="p">:</span> <span class="n">tanh</span><span class="p">,</span>
            <span class="s2">&quot;sigmoid&quot;</span><span class="p">:</span> <span class="n">logistic</span><span class="p">,</span>
            <span class="s2">&quot;relu&quot;</span><span class="p">:</span> <span class="n">rectify</span><span class="p">,</span>
            <span class="s2">&quot;tanh3&quot;</span><span class="p">:</span> <span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">tanh</span><span class="p">(</span><span class="n">cmult</span><span class="p">(</span><span class="n">cmult</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="p">),</span> <span class="n">x</span><span class="p">))),</span>
        <span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">activation</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">activations</span><span class="p">[</span><span class="n">options</span><span class="o">.</span><span class="n">activation</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">blstm_flag</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">blstmFlag</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">labels_flag</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">labelsFlag</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">costaug_flag</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">costaugFlag</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bibi_flag</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">bibiFlag</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">lstm_dims</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">wdims</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">wembedding_dims</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pdims</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">pembedding_dims</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rdims</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">rembedding_dims</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">layers</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">lstm_layers</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">words_count</span> <span class="o">=</span> <span class="n">vocab</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">vocab</span> <span class="o">=</span> <span class="p">{</span><span class="n">word</span><span class="p">:</span> <span class="n">ind</span> <span class="o">+</span> <span class="mi">3</span> <span class="k">for</span> <span class="n">word</span><span class="p">,</span> <span class="n">ind</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">w2i</span><span class="o">.</span><span class="n">items</span><span class="p">())}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pos</span> <span class="o">=</span> <span class="p">{</span><span class="n">word</span><span class="p">:</span> <span class="n">ind</span> <span class="o">+</span> <span class="mi">3</span> <span class="k">for</span> <span class="n">ind</span><span class="p">,</span> <span class="n">word</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">pos</span><span class="p">)}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rels</span> <span class="o">=</span> <span class="p">{</span><span class="n">word</span><span class="p">:</span> <span class="n">ind</span> <span class="k">for</span> <span class="n">ind</span><span class="p">,</span> <span class="n">word</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rels</span><span class="p">)}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">irels</span> <span class="o">=</span> <span class="n">rels</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bibi_flag</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">builders</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">LSTMBuilder</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">wdims</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">pdims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">),</span>
                <span class="n">LSTMBuilder</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">wdims</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">pdims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">),</span>
            <span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">bbuilders</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">LSTMBuilder</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">),</span>
                <span class="n">LSTMBuilder</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">),</span>
            <span class="p">]</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">layers</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">builders</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">LSTMBuilder</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">layers</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">wdims</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">pdims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">),</span>
                <span class="n">LSTMBuilder</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">layers</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">wdims</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">pdims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">),</span>
            <span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">builders</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">SimpleRNNBuilder</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">wdims</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">pdims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">),</span>
                <span class="n">SimpleRNNBuilder</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">wdims</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">pdims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">),</span>
            <span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">hidden_units</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">hidden_units</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">hidden2_units</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">hidden2_units</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">vocab</span><span class="p">[</span><span class="s2">&quot;*PAD*&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="s2">&quot;*PAD*&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">vocab</span><span class="p">[</span><span class="s2">&quot;*INITIAL*&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="s2">&quot;*INITIAL*&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">wlookup</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_lookup_parameters</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">vocab</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">wdims</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">plookup</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_lookup_parameters</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">pos</span><span class="p">)</span> <span class="o">+</span> <span class="mi">3</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">pdims</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">rlookup</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_lookup_parameters</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">rels</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">rdims</span><span class="p">))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">hid_layer_foh</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">hidden_units</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span> <span class="o">*</span> <span class="mi">2</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">hid_layer_fom</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">hidden_units</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span> <span class="o">*</span> <span class="mi">2</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">hid_bias</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">hidden_units</span><span class="p">))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">hid2_layer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">hidden2_units</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">hidden_units</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">hid2_bias</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">hidden2_units</span><span class="p">))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">out_layer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">(</span>
            <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">hidden2_units</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hidden2_units</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">hidden_units</span><span class="p">)</span>
        <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">labels_flag</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rhid_layer_foh</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">hidden_units</span><span class="p">,</span> <span class="mi">2</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rhid_layer_fom</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">hidden_units</span><span class="p">,</span> <span class="mi">2</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">ldims</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rhid_bias</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">hidden_units</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rhid2_layer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">hidden2_units</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">hidden_units</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rhid2_bias</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">hidden2_units</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rout_layer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">(</span>
                <span class="p">(</span>
                    <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">irels</span><span class="p">),</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">hidden2_units</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hidden2_units</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">hidden_units</span><span class="p">,</span>
                <span class="p">)</span>
            <span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rout_bias</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">add_parameters</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">irels</span><span class="p">)))</span>

    <span class="k">def</span> <span class="nf">_get_expr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sentence</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">):</span>
        <span class="c1"># pylint: disable=missing-docstring</span>
        <span class="k">if</span> <span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">headfov</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">headfov</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hid_layer_foh</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span> <span class="o">*</span> <span class="n">concatenate</span><span class="p">(</span>
                <span class="p">[</span><span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">modfov</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">modfov</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">hid_layer_fom</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span> <span class="o">*</span> <span class="n">concatenate</span><span class="p">(</span>
                <span class="p">[</span><span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hidden2_units</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">out_layer</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">hid2_bias</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span>
                <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">hid2_layer</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span>
                <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation</span><span class="p">(</span><span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">headfov</span> <span class="o">+</span> <span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">modfov</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">hid_bias</span><span class="o">.</span><span class="n">expr</span><span class="p">())</span>
            <span class="p">)</span>  <span class="c1"># + self.outBias</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">out_layer</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation</span><span class="p">(</span>
                <span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">headfov</span> <span class="o">+</span> <span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">modfov</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">hid_bias</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span>
            <span class="p">)</span>  <span class="c1"># + self.outBias</span>
        <span class="k">return</span> <span class="n">output</span>

    <span class="k">def</span> <span class="nf">_evaluate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sentence</span><span class="p">):</span>
        <span class="c1"># pylint: disable=missing-docstring</span>
        <span class="n">exprs</span> <span class="o">=</span> <span class="p">[</span>
            <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_get_expr</span><span class="p">(</span><span class="n">sentence</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">)</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">sentence</span><span class="p">))]</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">sentence</span><span class="p">))</span>
        <span class="p">]</span>
        <span class="n">scores</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="n">output</span><span class="o">.</span><span class="n">scalar_value</span><span class="p">()</span> <span class="k">for</span> <span class="n">output</span> <span class="ow">in</span> <span class="n">exprsRow</span><span class="p">]</span> <span class="k">for</span> <span class="n">exprsRow</span> <span class="ow">in</span> <span class="n">exprs</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">scores</span><span class="p">,</span> <span class="n">exprs</span>

    <span class="k">def</span> <span class="nf">_evaluate_label</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sentence</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">):</span>
        <span class="c1"># pylint: disable=missing-docstring</span>
        <span class="k">if</span> <span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">rheadfov</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">rheadfov</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rhid_layer_foh</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span> <span class="o">*</span> <span class="n">concatenate</span><span class="p">(</span>
                <span class="p">[</span><span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span>
            <span class="p">)</span>
        <span class="k">if</span> <span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">rmodfov</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">rmodfov</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rhid_layer_fom</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span> <span class="o">*</span> <span class="n">concatenate</span><span class="p">(</span>
                <span class="p">[</span><span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">hidden2_units</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">output</span> <span class="o">=</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">rout_layer</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span>
                <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">rhid2_bias</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span>
                    <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">rhid2_layer</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span>
                    <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation</span><span class="p">(</span>
                        <span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">rheadfov</span> <span class="o">+</span> <span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">rmodfov</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">rhid_bias</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span>
                    <span class="p">)</span>
                <span class="p">)</span>
                <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">rout_bias</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span>
            <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">output</span> <span class="o">=</span> <span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">rout_layer</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span>
                <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">activation</span><span class="p">(</span>
                    <span class="n">sentence</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">rheadfov</span> <span class="o">+</span> <span class="n">sentence</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="o">.</span><span class="n">rmodfov</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">rhid_bias</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span>
                <span class="p">)</span>
                <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">rout_bias</span><span class="o">.</span><span class="n">expr</span><span class="p">()</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="n">output</span><span class="o">.</span><span class="n">value</span><span class="p">(),</span> <span class="n">output</span>

<div class="viewcode-block" id="MSTParserLSTM.predict"><a class="viewcode-back" href="../../../../generated_api/nlp_architect.models.bist.html#nlp_architect.models.bist.mstlstm.MSTParserLSTM.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conll_path</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">conll</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># pylint: disable=missing-docstring</span>
        <span class="k">if</span> <span class="n">conll</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">conll</span> <span class="o">=</span> <span class="n">read_conll</span><span class="p">(</span><span class="n">conll_path</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">sentence</span> <span class="ow">in</span> <span class="n">conll</span><span class="p">:</span>
            <span class="n">conll_sentence</span> <span class="o">=</span> <span class="p">[</span><span class="n">entry</span> <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">sentence</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="n">ConllEntry</span><span class="p">)]</span>

            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">conll_sentence</span><span class="p">:</span>
                <span class="n">wordvec</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">wlookup</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">vocab</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">norm</span><span class="p">,</span> <span class="mi">0</span><span class="p">))]</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">wdims</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="kc">None</span>
                <span class="p">)</span>
                <span class="n">posvec</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">plookup</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="n">entry</span><span class="o">.</span><span class="n">pos</span><span class="p">])]</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pdims</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="kc">None</span>
                <span class="n">entry</span><span class="o">.</span><span class="n">vec</span> <span class="o">=</span> <span class="n">concatenate</span><span class="p">([</span><span class="n">_f</span> <span class="k">for</span> <span class="n">_f</span> <span class="ow">in</span> <span class="p">[</span><span class="n">wordvec</span><span class="p">,</span> <span class="n">posvec</span><span class="p">,</span> <span class="kc">None</span><span class="p">]</span> <span class="k">if</span> <span class="n">_f</span><span class="p">])</span>

                <span class="n">entry</span><span class="o">.</span><span class="n">lstms</span> <span class="o">=</span> <span class="p">[</span><span class="n">entry</span><span class="o">.</span><span class="n">vec</span><span class="p">,</span> <span class="n">entry</span><span class="o">.</span><span class="n">vec</span><span class="p">]</span>
                <span class="n">entry</span><span class="o">.</span><span class="n">headfov</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="n">entry</span><span class="o">.</span><span class="n">modfov</span> <span class="o">=</span> <span class="kc">None</span>

                <span class="n">entry</span><span class="o">.</span><span class="n">rheadfov</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="n">entry</span><span class="o">.</span><span class="n">rmodfov</span> <span class="o">=</span> <span class="kc">None</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">blstm_flag</span><span class="p">:</span>
                <span class="n">lstm_forward</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">builders</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">initial_state</span><span class="p">()</span>
                <span class="n">lstm_backward</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">builders</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">initial_state</span><span class="p">()</span>

                <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">rentry</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">,</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">)):</span>
                    <span class="n">lstm_forward</span> <span class="o">=</span> <span class="n">lstm_forward</span><span class="o">.</span><span class="n">add_input</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">vec</span><span class="p">)</span>
                    <span class="n">lstm_backward</span> <span class="o">=</span> <span class="n">lstm_backward</span><span class="o">.</span><span class="n">add_input</span><span class="p">(</span><span class="n">rentry</span><span class="o">.</span><span class="n">vec</span><span class="p">)</span>

                    <span class="n">entry</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">lstm_forward</span><span class="o">.</span><span class="n">output</span><span class="p">()</span>
                    <span class="n">rentry</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">lstm_backward</span><span class="o">.</span><span class="n">output</span><span class="p">()</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bibi_flag</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">conll_sentence</span><span class="p">:</span>
                        <span class="n">entry</span><span class="o">.</span><span class="n">vec</span> <span class="o">=</span> <span class="n">concatenate</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">lstms</span><span class="p">)</span>

                    <span class="n">blstm_forward</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbuilders</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">initial_state</span><span class="p">()</span>
                    <span class="n">blstm_backward</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbuilders</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">initial_state</span><span class="p">()</span>

                    <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">rentry</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">,</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">)):</span>
                        <span class="n">blstm_forward</span> <span class="o">=</span> <span class="n">blstm_forward</span><span class="o">.</span><span class="n">add_input</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">vec</span><span class="p">)</span>
                        <span class="n">blstm_backward</span> <span class="o">=</span> <span class="n">blstm_backward</span><span class="o">.</span><span class="n">add_input</span><span class="p">(</span><span class="n">rentry</span><span class="o">.</span><span class="n">vec</span><span class="p">)</span>

                        <span class="n">entry</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">blstm_forward</span><span class="o">.</span><span class="n">output</span><span class="p">()</span>
                        <span class="n">rentry</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">blstm_backward</span><span class="o">.</span><span class="n">output</span><span class="p">()</span>

            <span class="n">scores</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_evaluate</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">)</span>
            <span class="n">heads</span> <span class="o">=</span> <span class="n">decoder</span><span class="o">.</span><span class="n">parse_proj</span><span class="p">(</span><span class="n">scores</span><span class="p">)</span>

            <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">head</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">,</span> <span class="n">heads</span><span class="p">):</span>
                <span class="n">entry</span><span class="o">.</span><span class="n">pred_parent_id</span> <span class="o">=</span> <span class="n">head</span>
                <span class="n">entry</span><span class="o">.</span><span class="n">pred_relation</span> <span class="o">=</span> <span class="s2">&quot;_&quot;</span>

            <span class="n">dump</span> <span class="o">=</span> <span class="kc">False</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">labels_flag</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">modifier</span><span class="p">,</span> <span class="n">head</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">heads</span><span class="p">[</span><span class="mi">1</span><span class="p">:]):</span>
                    <span class="n">scores</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_evaluate_label</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">,</span> <span class="n">head</span><span class="p">,</span> <span class="n">modifier</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
                    <span class="n">conll_sentence</span><span class="p">[</span><span class="n">modifier</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">pred_relation</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">irels</span><span class="p">[</span>
                        <span class="nb">max</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">scores</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">1</span><span class="p">))[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="p">]</span>

            <span class="n">renew_cg</span><span class="p">()</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">dump</span><span class="p">:</span>
                <span class="k">yield</span> <span class="n">sentence</span></div>

<div class="viewcode-block" id="MSTParserLSTM.train"><a class="viewcode-back" href="../../../../generated_api/nlp_architect.models.bist.html#nlp_architect.models.bist.mstlstm.MSTParserLSTM.train">[docs]</a>    <span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">conll_path</span><span class="p">):</span>
        <span class="c1"># pylint: disable=invalid-name</span>
        <span class="c1"># pylint: disable=missing-docstring</span>
        <span class="n">eloss</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">mloss</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">eerrors</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">etotal</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>

        <span class="n">shuffled_data</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">read_conll</span><span class="p">(</span><span class="n">conll_path</span><span class="p">))</span>
        <span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">shuffled_data</span><span class="p">)</span>
        <span class="n">errs</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">lerrs</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">i_sentence</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="k">for</span> <span class="n">sentence</span> <span class="ow">in</span> <span class="n">shuffled_data</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">i_sentence</span> <span class="o">%</span> <span class="mi">100</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">i_sentence</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="nb">print</span><span class="p">(</span>
                    <span class="s2">&quot;Processing sentence number:&quot;</span><span class="p">,</span>
                    <span class="n">i_sentence</span><span class="p">,</span>
                    <span class="s2">&quot;Loss:&quot;</span><span class="p">,</span>
                    <span class="n">eloss</span> <span class="o">/</span> <span class="n">etotal</span><span class="p">,</span>
                    <span class="s2">&quot;Errors:&quot;</span><span class="p">,</span>
                    <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">eerrors</span><span class="p">))</span> <span class="o">/</span> <span class="n">etotal</span><span class="p">,</span>
                    <span class="s2">&quot;Time&quot;</span><span class="p">,</span>
                    <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">start</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="n">start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
                <span class="n">eerrors</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="n">eloss</span> <span class="o">=</span> <span class="mf">0.0</span>
                <span class="n">etotal</span> <span class="o">=</span> <span class="mi">0</span>

            <span class="n">conll_sentence</span> <span class="o">=</span> <span class="p">[</span><span class="n">entry</span> <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">sentence</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="n">ConllEntry</span><span class="p">)]</span>

            <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">conll_sentence</span><span class="p">:</span>
                <span class="n">c</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">words_count</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">norm</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
                <span class="n">drop_flag</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="o">&lt;</span> <span class="p">(</span><span class="n">c</span> <span class="o">/</span> <span class="p">(</span><span class="mf">0.25</span> <span class="o">+</span> <span class="n">c</span><span class="p">))</span>
                <span class="n">wordvec</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">wlookup</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">vocab</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">norm</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span> <span class="k">if</span> <span class="n">drop_flag</span> <span class="k">else</span> <span class="mi">0</span><span class="p">]</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">wdims</span> <span class="o">&gt;</span> <span class="mi">0</span>
                    <span class="k">else</span> <span class="kc">None</span>
                <span class="p">)</span>
                <span class="n">posvec</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">plookup</span><span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">[</span><span class="n">entry</span><span class="o">.</span><span class="n">pos</span><span class="p">])]</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pdims</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="kc">None</span>

                <span class="n">entry</span><span class="o">.</span><span class="n">vec</span> <span class="o">=</span> <span class="n">concatenate</span><span class="p">([</span><span class="n">_f</span> <span class="k">for</span> <span class="n">_f</span> <span class="ow">in</span> <span class="p">[</span><span class="n">wordvec</span><span class="p">,</span> <span class="n">posvec</span><span class="p">,</span> <span class="kc">None</span><span class="p">]</span> <span class="k">if</span> <span class="n">_f</span><span class="p">])</span>

                <span class="n">entry</span><span class="o">.</span><span class="n">lstms</span> <span class="o">=</span> <span class="p">[</span><span class="n">entry</span><span class="o">.</span><span class="n">vec</span><span class="p">,</span> <span class="n">entry</span><span class="o">.</span><span class="n">vec</span><span class="p">]</span>
                <span class="n">entry</span><span class="o">.</span><span class="n">headfov</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="n">entry</span><span class="o">.</span><span class="n">modfov</span> <span class="o">=</span> <span class="kc">None</span>

                <span class="n">entry</span><span class="o">.</span><span class="n">rheadfov</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="n">entry</span><span class="o">.</span><span class="n">rmodfov</span> <span class="o">=</span> <span class="kc">None</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">blstm_flag</span><span class="p">:</span>
                <span class="n">lstm_forward</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">builders</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">initial_state</span><span class="p">()</span>
                <span class="n">lstm_backward</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">builders</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">initial_state</span><span class="p">()</span>

                <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">rentry</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">,</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">)):</span>
                    <span class="n">lstm_forward</span> <span class="o">=</span> <span class="n">lstm_forward</span><span class="o">.</span><span class="n">add_input</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">vec</span><span class="p">)</span>
                    <span class="n">lstm_backward</span> <span class="o">=</span> <span class="n">lstm_backward</span><span class="o">.</span><span class="n">add_input</span><span class="p">(</span><span class="n">rentry</span><span class="o">.</span><span class="n">vec</span><span class="p">)</span>

                    <span class="n">entry</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">lstm_forward</span><span class="o">.</span><span class="n">output</span><span class="p">()</span>
                    <span class="n">rentry</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">lstm_backward</span><span class="o">.</span><span class="n">output</span><span class="p">()</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bibi_flag</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">conll_sentence</span><span class="p">:</span>
                        <span class="n">entry</span><span class="o">.</span><span class="n">vec</span> <span class="o">=</span> <span class="n">concatenate</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">lstms</span><span class="p">)</span>

                    <span class="n">blstm_forward</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbuilders</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">initial_state</span><span class="p">()</span>
                    <span class="n">blstm_backward</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbuilders</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">initial_state</span><span class="p">()</span>

                    <span class="k">for</span> <span class="n">entry</span><span class="p">,</span> <span class="n">rentry</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">,</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">)):</span>
                        <span class="n">blstm_forward</span> <span class="o">=</span> <span class="n">blstm_forward</span><span class="o">.</span><span class="n">add_input</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">vec</span><span class="p">)</span>
                        <span class="n">blstm_backward</span> <span class="o">=</span> <span class="n">blstm_backward</span><span class="o">.</span><span class="n">add_input</span><span class="p">(</span><span class="n">rentry</span><span class="o">.</span><span class="n">vec</span><span class="p">)</span>

                        <span class="n">entry</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">blstm_forward</span><span class="o">.</span><span class="n">output</span><span class="p">()</span>
                        <span class="n">rentry</span><span class="o">.</span><span class="n">lstms</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">blstm_backward</span><span class="o">.</span><span class="n">output</span><span class="p">()</span>

            <span class="n">scores</span><span class="p">,</span> <span class="n">exprs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_evaluate</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">)</span>
            <span class="n">gold</span> <span class="o">=</span> <span class="p">[</span><span class="n">entry</span><span class="o">.</span><span class="n">parent_id</span> <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">conll_sentence</span><span class="p">]</span>
            <span class="n">heads</span> <span class="o">=</span> <span class="n">decoder</span><span class="o">.</span><span class="n">parse_proj</span><span class="p">(</span><span class="n">scores</span><span class="p">,</span> <span class="n">gold</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">costaug_flag</span> <span class="k">else</span> <span class="kc">None</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">labels_flag</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">modifier</span><span class="p">,</span> <span class="n">head</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">gold</span><span class="p">[</span><span class="mi">1</span><span class="p">:]):</span>
                    <span class="n">rscores</span><span class="p">,</span> <span class="n">rexprs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_evaluate_label</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">,</span> <span class="n">head</span><span class="p">,</span> <span class="n">modifier</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
                    <span class="n">gold_label_ind</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rels</span><span class="p">[</span><span class="n">conll_sentence</span><span class="p">[</span><span class="n">modifier</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">relation</span><span class="p">]</span>
                    <span class="n">wrong_label_ind</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span>
                        <span class="p">(</span>
                            <span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="n">scr</span><span class="p">)</span>
                            <span class="k">for</span> <span class="n">label</span><span class="p">,</span> <span class="n">scr</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">rscores</span><span class="p">)</span>
                            <span class="k">if</span> <span class="n">label</span> <span class="o">!=</span> <span class="n">gold_label_ind</span>
                        <span class="p">),</span>
                        <span class="n">key</span><span class="o">=</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span>
                    <span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
                    <span class="k">if</span> <span class="n">rscores</span><span class="p">[</span><span class="n">gold_label_ind</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">rscores</span><span class="p">[</span><span class="n">wrong_label_ind</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span>
                        <span class="n">lerrs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">rexprs</span><span class="p">[</span><span class="n">wrong_label_ind</span><span class="p">]</span> <span class="o">-</span> <span class="n">rexprs</span><span class="p">[</span><span class="n">gold_label_ind</span><span class="p">])</span>

            <span class="n">e</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="mi">1</span> <span class="k">for</span> <span class="n">h</span><span class="p">,</span> <span class="n">g</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">heads</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="n">gold</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span> <span class="k">if</span> <span class="n">h</span> <span class="o">!=</span> <span class="n">g</span><span class="p">])</span>
            <span class="n">eerrors</span> <span class="o">+=</span> <span class="n">e</span>
            <span class="k">if</span> <span class="n">e</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">loss</span> <span class="o">=</span> <span class="p">[</span>
                    <span class="p">(</span><span class="n">exprs</span><span class="p">[</span><span class="n">h</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">exprs</span><span class="p">[</span><span class="n">g</span><span class="p">][</span><span class="n">i</span><span class="p">])</span>
                    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="n">g</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">heads</span><span class="p">,</span> <span class="n">gold</span><span class="p">))</span>
                    <span class="k">if</span> <span class="n">h</span> <span class="o">!=</span> <span class="n">g</span>
                <span class="p">]</span>  <span class="c1"># * (1.0/float(e))</span>
                <span class="n">eloss</span> <span class="o">+=</span> <span class="n">e</span>
                <span class="n">mloss</span> <span class="o">+=</span> <span class="n">e</span>
                <span class="n">errs</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">loss</span><span class="p">)</span>

            <span class="n">etotal</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">conll_sentence</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">i_sentence</span> <span class="o">%</span> <span class="mi">1</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">errs</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">lerrs</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">errs</span> <span class="ow">or</span> <span class="n">lerrs</span><span class="p">:</span>
                    <span class="n">eerrs</span> <span class="o">=</span> <span class="n">esum</span><span class="p">(</span><span class="n">errs</span> <span class="o">+</span> <span class="n">lerrs</span><span class="p">)</span>  <span class="c1"># * (1.0/(float(len(errs))))</span>
                    <span class="n">eerrs</span><span class="o">.</span><span class="n">scalar_value</span><span class="p">()</span>
                    <span class="n">eerrs</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">trainer</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>
                    <span class="n">errs</span> <span class="o">=</span> <span class="p">[]</span>
                    <span class="n">lerrs</span> <span class="o">=</span> <span class="p">[]</span>

                <span class="n">renew_cg</span><span class="p">()</span>

            <span class="n">i_sentence</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">if</span> <span class="n">errs</span><span class="p">:</span>
            <span class="n">eerrs</span> <span class="o">=</span> <span class="n">esum</span><span class="p">(</span><span class="n">errs</span> <span class="o">+</span> <span class="n">lerrs</span><span class="p">)</span>  <span class="c1"># * (1.0/(float(len(errs))))</span>
            <span class="n">eerrs</span><span class="o">.</span><span class="n">scalar_value</span><span class="p">()</span>
            <span class="n">eerrs</span><span class="o">.</span><span class="n">backward</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">trainer</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>

            <span class="n">renew_cg</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">trainer</span><span class="o">.</span><span class="n">update</span><span class="p">()</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Loss: &quot;</span><span class="p">,</span> <span class="n">mloss</span> <span class="o">/</span> <span class="n">i_sentence</span><span class="p">)</span></div>

<div class="viewcode-block" id="MSTParserLSTM.save"><a class="viewcode-back" href="../../../../generated_api/nlp_architect.models.bist.html#nlp_architect.models.bist.mstlstm.MSTParserLSTM.save">[docs]</a>    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span></div>

<div class="viewcode-block" id="MSTParserLSTM.load"><a class="viewcode-back" href="../../../../generated_api/nlp_architect.models.bist.html#nlp_architect.models.bist.mstlstm.MSTParserLSTM.load">[docs]</a>    <span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filename</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">populate</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span></div></div>


<span class="k">def</span> <span class="nf">_dict_to_obj</span><span class="p">(</span><span class="n">dic</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;Object&quot;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Return an object form of a dictionary.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">namedtuple</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">dic</span><span class="o">.</span><span class="n">keys</span><span class="p">())(</span><span class="o">*</span><span class="n">dic</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>